package com.dongdongshop.service.impl;

import com.dongdongshop.data.Page;
import com.dongdongshop.mapper.TbItemCatMapper;
import com.dongdongshop.pojo.TbItemCat;
import com.dongdongshop.service.ItemCatService;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.dubbo.config.annotation.DubboService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;

@Service
@DubboService
public class ItemCatImpl implements ItemCatService {
    @Autowired
    private TbItemCatMapper tbItemCatMapper;

    @Override
    public Page<TbItemCat> toShow(Long parentId, Integer pageNum, Integer pageSize) {
        PageHelper.startPage(pageNum,pageSize);
        List<TbItemCat> list = tbItemCatMapper.toShow(parentId);
        PageInfo<TbItemCat> pageInfo = new PageInfo<>(list);
        Page<TbItemCat> page = new Page(pageInfo.getPageNum(),pageInfo.getPageSize(),pageInfo.getPages(), pageInfo.getList());
        return page;
    }

    @Override
    public void add(TbItemCat tbItemCat) {
        tbItemCatMapper.insertSelective(tbItemCat);
    }

    @Override
    public TbItemCat toUpdate(Long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public void updateInfo(TbItemCat tbItemCat) {
        tbItemCatMapper.updateByPrimaryKeySelective(tbItemCat);
    }

    @Override
    @Transactional
    public Integer deleteInfo(Long[] ids) {
        for (int i = 0;ids.length > i;i++) {
            List<TbItemCat> list = tbItemCatMapper.selectByPid(ids[i]);
            if (list == null || list.size() == 0) {
                tbItemCatMapper.deleteByPrimaryKey(ids[i]);
            }else {
                return 2;
            }
        }
        return 1;
    }

    @Override
    public List<TbItemCat> selectByPid(Long id) {
        return tbItemCatMapper.selectByPid(id);
    }

    @Override
    public TbItemCat selectById(Long id) {
        return tbItemCatMapper.selectByPrimaryKey(id);
    }

    @Override
    public List<TbItemCat> getInfo() {
        return tbItemCatMapper.selectByExample(null);
    }

    @Override
    public TbItemCat selectBycategory1Id(Long category1Id) {
        return tbItemCatMapper.selectByPrimaryKey(category1Id);
    }

    @Override
    public TbItemCat selectBycategory2Id(Long category2Id) {
        return tbItemCatMapper.selectByPrimaryKey(category2Id);
    }

    @Override
    public TbItemCat selectBycategory3Id(Long category3Id) {
        return tbItemCatMapper.selectByPrimaryKey(category3Id);
    }
}
